﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for ChiTietDeThi
/// </summary>
public class ChiTietDeThiDAL
{
    public static object IsNull(object checkValue, object replaceValue)
    {
        if (checkValue.Equals(System.DBNull.Value) || checkValue == null)
            return replaceValue;
        return checkValue;
    }

    public static List<ChiTietDeThiINFO> listAll()
    {
        string sql = "select * from ChiTietDeThi";
        SqlDataReader dr = SQLHelper.ExecuteReader(Conn.ConnectionString, CommandType.Text, sql, null);
        return SQLHelper.SetListObjects<ChiTietDeThiINFO>(dr);
    }

    public static List<CauHoiINFO> listByCauHoi(int MaCauHoi)
    {
        string sql = "select ch.* from ChiTietDeThi as ctdt Inner Join CauHoi as ch on ctdt.MaCH = ch.MaCH Where ctdt.MaDe = " + MaCauHoi;
        SqlDataReader dr = SQLHelper.ExecuteReader(Conn.ConnectionString, CommandType.Text, sql, null);
        return SQLHelper.SetListObjects<CauHoiINFO>(dr);
    }

    public static List<CauHoiINFO> listByCauHoiClient(int MaDe)
    {
        string sql = "select ch.* from ChiTietDeThi as ctdt Inner Join CauHoi as ch on ctdt.MaCH = ch.MaCH Where ctdt.MaDe = " + MaDe + " ORDER BY NEWID()";
        SqlDataReader dr = SQLHelper.ExecuteReader(Conn.ConnectionString, CommandType.Text, sql, null);
        return SQLHelper.SetListObjects<CauHoiINFO>(dr);
    }

    public static ChiTietDeThiINFO GetInfo(int MaCH, int MaDe)
    {
        string sql = "select * from ChiTietDeThi where MaCH =" + MaCH + " and MaDe= "+ MaDe +"";
        SqlDataReader dr = SQLHelper.ExecuteReader(Conn.ConnectionString, CommandType.Text, sql, null);
        return SQLHelper.SetObject<ChiTietDeThiINFO>(dr);
    }

    public static int Insert(ChiTietDeThiINFO obj)
    {
        string sql = "INSERT INTO ChiTietDeThi (MaCH,MaDe) VALUES (" + obj.MaCH + "," + obj.MaDe + ")";
        return SQLHelper.ExecuteNonQuery(Conn.ConnectionString, CommandType.Text, sql, null);
    }
    public static int Insert(ChiTietDeThiINFO obj, SqlTransaction trans)
    {
        string sql = "INSERT INTO ChiTietDeThi (MaCH,MaDe) VALUES (" + obj.MaCH + "," + obj.MaDe + ")";
        return SQLHelper.ExecuteNonQuery(trans, CommandType.Text, sql, null);
    }

    public static int Delete(int MaCH, int MaDe)
    {
        string sql = "Delete from ChiTietDeThi where MaCH =" + MaCH + " and MaDe= " + MaDe + "";
        return SQLHelper.ExecuteNonQuery(Conn.ConnectionString, CommandType.Text, sql, null);
    }

    public static int Delete(int MaDe)
    {
        string sql = "Delete from ChiTietDeThi where MaDe= " + MaDe + "";
        return SQLHelper.ExecuteNonQuery(Conn.ConnectionString, CommandType.Text, sql, null);
    }

    public static int ThemCauHoiNhanh(int MaDe, List<CauHoiINFO> dsCauHoi, SqlTransaction trans)
    {
        foreach (var item in dsCauHoi)
        {
            ChiTietDeThiINFO obj = new ChiTietDeThiINFO();
            obj.MaDe = MaDe;
            obj.MaCH = item.MaCH;
            if (Insert(obj, trans) <= 0)
            {
                return 0;
            }
        }
        return 1;
    }
}